const crypto = require('crypto');

const algorithm = 'aes-128-cbc';

var signer = {
	
	encrypt: function(plainText, keyBase64, nonce) {

	    var key = Buffer.from(keyBase64, 'hex');
	    var iv = Buffer.from(nonce);

	    var cipher = crypto.createCipheriv(algorithm, key, iv);
	    var cipherTxt = cipher.update(plainText, 'utf8', 'base64')
		cipherTxt += cipher.final('base64');
		return cipherTxt;
	},

	decrypt: function(messagebase64, keyBase64, nonce) {

	    var key = Buffer.from(keyBase64, 'hex');
	    var iv = Buffer.from(nonce);

	    var decipher = crypto.createDecipheriv(algorithm, key, iv);
	    var plainTxt = decipher.update(messagebase64, 'base64');
	    plainTxt += decipher.final();
		return plainTxt;
	}

};

module.exports = signer;